package com.ruoyi.hotel.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.hotel.domain.DeviceInfo;
import com.ruoyi.hotel.mapper.DeviceInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hotel.mapper.PurchaseMapper;
import com.ruoyi.hotel.domain.Purchase;
import com.ruoyi.hotel.service.IPurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import java.util.Arrays;

/**
 * 采购管理Service业务层处理
 * 
 * @author zzd
 * @date 2025-03-05
 */
@Service
public class PurchaseServiceImpl extends ServiceImpl<PurchaseMapper, Purchase> implements IPurchaseService
{
    @Autowired
    private PurchaseMapper purchaseMapper;

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    /**
     * 查询采购管理
     * 
     * @param id 采购管理主键
     * @return 采购管理
     */
    @Override
    public Purchase selectPurchaseById(String id)
    {
                return getById(id);
    }

    /**
     * 查询采购管理列表
     * 
     * @param purchase 采购管理
     * @return 采购管理
     */
    @Override
    public List<Purchase> selectPurchaseList(Purchase purchase)
    {
        return purchaseMapper.selectPurchaseList(purchase);
    }

    /**
     * 新增采购管理
     * 
     * @param purchase 采购管理
     * @return 结果
     */
    @Override
    public int insertPurchase(Purchase purchase)
    {
        UUID uuid = UUID.fastUUID();
        purchase.setId(uuid.toString());
        purchase.setStatus("2");
        purchase.setStatusText("未审批");
        purchase.setCreateTime(DateUtils.getNowDate());
                return save(purchase) ? 1 : 0;
    }

    /**
     * 修改采购管理
     * 
     * @param purchase 采购管理
     * @return 结果
     */
    @Override
    public int updatePurchase(Purchase purchase)
    {
        String status = purchase.getStatus();
        if (status .equals("1")){
            purchase.setStatusText("驳回");
        }else if (status.equals("2")){
            purchase.setStatusText("未审批");
        }else if (status.equals("3")){
            purchase.setStatusText("已审批");
            DeviceInfo deviceInfo = new DeviceInfo();
            String id = UUID.fastUUID().toString();
            String deviceName = purchase.getPurchaseName();
            String deviceLocation = purchase.getDeviceLocation();
            String deviceCode = purchase.getDeviceCode();
            deviceInfo.setDeviceCode(deviceCode);
            deviceInfo.setId(id);
            deviceInfo.setDeviceName(deviceName);
            deviceInfo.setDeviceLocation(deviceLocation);
            deviceInfo.setDeviceStatus("1");
            deviceInfo.setDeviceStatusText("正常");
            deviceInfoMapper.insert(deviceInfo);
        }
        purchase.setUpdateTime(DateUtils.getNowDate());
                return updateById(purchase) ? 1 : 0;
    }

    /**
     * 批量删除采购管理
     * 
     * @param ids 需要删除的采购管理主键
     * @return 结果
     */
    @Override
    public int deletePurchaseByIds(String[] ids)
    {
                return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除采购管理信息
     * 
     * @param id 采购管理主键
     * @return 结果
     */
    @Override
    public int deletePurchaseById(String id)
    {
                return removeById(id) ? 1 : 0;
    }

    @Override
    public List<Purchase> selectManagePurchaseList(Purchase purchase) {


        this.list();
        return purchaseMapper.selectManagePurchaseList(purchase);
    }
}
